package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.OmsCompanyAddress;
import com.macro.mall.model.OmsCompanyAddressExample;
import com.macro.mall.model.UmsMemberReceiveAddress;
import com.macro.mall.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址管理Controller
 * Created by macro on 2018/10/18.
 */
@RestController
@Tag(name = "AddressController", description = "收货地址管理")
@RequestMapping("member/address")
public class AddressController {
    @Autowired
    private AddressService companyAddressService;

    /**
     * 地址详情
     * @return
     */
    @Operation(summary = "获取所有收货地址")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsMemberReceiveAddress>> list() {
        List<UmsMemberReceiveAddress> companyAddressList = companyAddressService.list();
        return CommonResult.success(companyAddressList);
    }

    /***
     * 新增收货地址
     * @param omsCompanyAddress
     * @return
     */
    @Operation(summary = "新增收货地址")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addAddr(@RequestBody UmsMemberReceiveAddress omsCompanyAddress) {
        int i = companyAddressService.insert(omsCompanyAddress);
        if (i>0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }





    /**
     * 删除收货地址
     * @param id
     */
    @Operation(summary = "删除收货地址")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delAddr(@PathVariable Long id) {
      int count =  companyAddressService.delect(id);
      if (count>0) {
          return CommonResult.success("删除成功");
      }
      return CommonResult.failed();
    }

    /**
     * 删除收货地址
     * @param id
     */
    @Operation(summary = "回显收货地址")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMemberReceiveAddress> selectAddr(@PathVariable Long id) {
        UmsMemberReceiveAddress umsMemberReceiveAddress  =  companyAddressService.selectAddr(id);
        return CommonResult.success(umsMemberReceiveAddress);
    }

    /**
     * 修改收货地址
     * @param
     */
    @Operation(summary = "修改收货地址")
    @RequestMapping(value = "/update/{umsMemberReceiveAddress}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult selectAddr(@RequestBody UmsMemberReceiveAddress umsMemberReceiveAddress) {
       int count =  companyAddressService.updateAddr(umsMemberReceiveAddress);
        if (count>0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed();
    }

//    @Operation(summary = "自动识别")
//    @RequestMapping(value = "/address", method = RequestMethod.POST)
//    @ResponseBody
//    public UmsMemberReceiveAddress abc(@RequestParam(value = "addrName") String addrName) {
//      return  companyAddressService.abc(addrName);
//    }

}
